package com.mind.core.util;

import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collections;
import java.util.Enumeration;

import org.apache.log4j.Logger;

public class NetUtil {
	private static final Logger logger = Logger.getLogger(NetUtil.class);
	 private static NetworkInterface getEth0NetworkInterface() {
	        Enumeration<NetworkInterface> nets;
	        NetworkInterface netInterfaceEth0 = null;
			try {
				nets = NetworkInterface.getNetworkInterfaces();
				 
			        for (Object obj : Collections.list(nets)) {
			            NetworkInterface netInterface = (NetworkInterface) obj;
			            if (netInterface.getName().equals("eth0")) {
			                netInterfaceEth0 = netInterface;
			            }
			        }
			} catch (SocketException e) {
				logger.error("", e);
			}
	        return netInterfaceEth0;
	    }
	    public static String getEth0IP() {
	    	NetworkInterface netInterface=getEth0NetworkInterface();
	        String ip = "";
	        if (netInterface != null) {
	            Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
	 
	            for (Object obj : Collections.list(addresses)) {
	                InetAddress inetAddress = (InetAddress) obj;
	                if (inetAddress instanceof Inet6Address) {
	                    continue;
	                } else {
	                    ip = inetAddress.getHostAddress();
	                }
	            }
	        }
	        return ip;
	    }
}
